<template>
	<view class="uni-container">
		<view class="userinfo">
			<view class="nickname-wrapper border">
				<view class="nickname-left">
					<text class="nickname-label">openid：</text>
				</view>
				<view class="nickname-right">
					<input type="text" class="nickname-input" v-model="openid" placeholder="用户openid" />
					<button type="primary" size="mini" plain="true" @click="wxLogin">获取用户openid</button>
				</view>
			</view>
			<view class="nickname-wrapper border">
				<view class="nickname-left">
					<text class="nickname-label">支付：</text>
				</view>
				<view class="nickname-right">
					<button type="primary" size="mini" plain="true" @click="pay">支付1分钱</button>
				</view>
			</view>
			<view class="nickname-wrapper border">
				<view class="nickname-left">
					<text class="nickname-label">订单查询：</text>
				</view>
				<view class="nickname-right">
					<input type="text" class="nickname-input" v-model="out_trade_no" placeholder="商户订单号" />
					<input type="text" class="nickname-input" v-model="transaction_id" placeholder="微信订单号" />
					<button type="primary" size="mini" plain="true" @click="checkOrder">查询订单状态</button>
				</view>
			</view>
			<view class="nickname-wrapper border">
				<view class="nickname-left">
					<text class="nickname-label">订单退款：</text>
				</view>
				<view class="nickname-right">
					<input type="text" class="nickname-input" v-model="out_trade_no" placeholder="商户订单号" />
					<input type="text" class="nickname-input" v-model="transaction_id" placeholder="微信订单号" />
					<input type="text" class="nickname-input" v-model="total_fee" placeholder="订单金额" />
					<input type="text" class="nickname-input" v-model="refund_fee" placeholder="退款金额" />
					<button type="primary" size="mini" plain="true" @click="refund">申请退款</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//url: 'http://www.tp6.com/index.php', 	//后台请求域名，需要修改你自己的本地或者线上域名
				url: 'http://www.tp8.com/index.php', //后台请求域名，需要修改你自己的本地或者线上域名
				openid: '',
				out_trade_no: '',
				transaction_id: '',
				total_fee: 0.01,
				refund_fee: 0.01
			};
		},
		onLoad(options) {
			//console.log(123);
		},
		onShow() {
			//console.log(456);
		},
		methods: {
			wxLogin() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						//console.log('登录成功，code为：' + res.code);
						// 获取临时登录凭证code
						uni.request({
							url: this.url + '/wechat/getOpenid',
							method: 'POST',
							data: {
								code: res.code
							},
							header: {},
							success: (res) => {
								//console.log(res.data);
								this.openid = res.data.data;
							}
						});
					},
					fail: (err) => {
						console.error('登录失败：', err);
					}
				});
			},
			//测试支付
			pay() {
				if (!this.openid) {
					uni.showToast({
						title: '请先获取用户openid',
						icon: 'none'
					});
					return false;
				}
				uni.request({
					url: this.url + '/wechat/pay',
					method: 'POST',
					data: {
						open_id: this.openid
					},
					header: {},
					success: (res) => {
						//console.log(res.data);
						if (res.data.code) {
							const payment = res.data.data;
							// 调起微信支付
							uni.requestPayment({
								provider: 'wxpay',
								nonceStr: payment.nonceStr,
								package: payment.package,
								paySign: payment.paySign,
								signType: payment.signType,
								timeStamp: payment.timeStamp,
								success: (res) => {
									console.log('success:' + JSON.stringify(res));
								},
								fail: (err) => {
									console.log('fail:' + JSON.stringify(err));
								}
							});
						} else {
							console.log('获取支付参数失败');
						}
					}
				});
			},
			//测试查询订单状态
			checkOrder() {
				if (!this.out_trade_no && !this.transaction_id) {
					uni.showToast({
						title: '商户订单号和微信订单号不能同时为空！',
						icon: 'none'
					});
					return false;
				}
				uni.request({
					url: this.url + '/wechat/checkOrder',
					method: 'POST',
					data: {
						out_trade_no: this.out_trade_no,
						transaction_id: this.transaction_id
					},
					header: {},
					success: (res) => {
						console.log(res.data);
					}
				});
			},
			//测试退款
			refund() {
				if (!this.out_trade_no && !this.transaction_id) {
					uni.showToast({
						title: '商户订单号和微信订单号不能同时为空！',
						icon: 'none'
					});
					return false;
				}
				if (!this.total_fee) {
					uni.showToast({
						title: '订单金额不能为空！',
						icon: 'none'
					});
					return false;
				}
				if (!this.refund_fee) {
					uni.showToast({
						title: '退款金额不能为空！',
						icon: 'none'
					});
					return false;
				}
				uni.request({
					url: this.url + '/wechat/refund',
					method: 'POST',
					data: {
						out_trade_no: this.out_trade_no,
						transaction_id: this.transaction_id,
						total_fee: this.total_fee,
						refund_fee: this.refund_fee
					},
					header: {},
					success: (res) => {
						console.log(res.data);
						/*if (res.data.code) {
							
						} else {
							console.log('获取支付参数失败');
						}*/
					}
				});
			},
		}
	};
</script>

<style lang="scss">
	.uni-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100%;
		padding: 20rpx;
	}

	.userinfo {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: #aaa;
		width: 90%;
	}

	.usermotto {
		margin-top: 200px;
	}

	.nickname-wrapper {
		display: flex;
		width: 100%;
		padding: 16px;
		box-sizing: border-box;
		border: .5px solid rgba(0, 0, 0, 0.1);
		color: black;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.nickname-left {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.nickname-right input {
		height: 60rpx;
		border: .5px solid rgba(0, 0, 0, 0.1);
		margin-bottom: 30rpx;
	}

	.nickname-label {
		width: 80px;
	}

	.nickname-input {
		flex: 1;
		font-size: 24rpx;
		padding: 0 10rpx;
		width: 380rpx;
	}
</style>